<template>
  <div class="user-page">
    <!-- 我的 -->
    <div class="fu">
      <img src="http://liufusong.top:8080/img/profile/bg.png" alt="" />
      <div class="My_info">
        <div class="tx">
          <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="" />
        </div>
        <div class="uname">{{ nickname }}</div>
        <van-button type="primary" to="/login" @click="logout">{{change}}</van-button>
        <div class="edit" v-if="change==='退出'">编辑个人资料 <van-icon name="play" /></div>
      </div>
    </div>

    <van-grid :column-num="3">
      <van-grid-item icon="star-o" text="我的收藏" to="/collect" />
      <van-grid-item icon="wap-home-o" text="我的出租" to="/rent" />
      <van-grid-item icon="clock-o" text="看房记录" />
      <van-grid-item icon="coupon-o" text="成为房主" />
      <van-grid-item icon="user-o" text="个人资料" />
      <van-grid-item icon="service-o" text="联系我们" />
    </van-grid>
    <!-- 广告区 -->
    <div class="ad">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { user, logout } from '@/api/user'
import { delToken } from '@/utils/storage'
export default {
  name: 'user-page',
  data () {
    return {
      nickname: '游客',
      change: '去登录'
    }
  },
  async created () {
    const { body } = await user()
    // console.log(res)
    this.nickname = body.nickname
    this.change = this.nickname === '游客' ? '去登录' : '退出'
  },
  methods: {
    async logout () {
      if (this.change === '退出') {
        await logout()
        delToken()
        this.$toast.success('退出成功')
        this.$router.push('/user')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.fu {
  position: relative;
  min-height: 300px;
  img {
    width: 100%;
  }
  .My_info {
    position: absolute;
    width: 80%;
    height: 55%;
    margin: 50px auto 0;
    background-color: #fff;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    box-shadow: 0 0 10px 3px #ddd;
    padding: 0 10px;
    text-align: center;
    font-size: 13px;
    .tx {
      position: relative;
      transform: translateY(-50%);
      border-radius: 50%;
      width: 70px;
      height: 70px;
      border: 5px solid #f5f5f5;
      display: inline-block;
      box-shadow: 0 2px 2px #bdbdbd;
      img {
        width: 100%;
        border-radius: 50%;
      }
    }
    .uname {
      margin-top: -30px;
      margin-bottom: 10px;
    }
    .edit {
      color: #999;
    }
  }
}
.van-hairline--top {
  margin-top: 10px;
}
.ad {
  margin-top: 10px;
  text-align: center;
  img {
    width: 93%;
  }
}
</style>
